import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:kq_flutter_core_widget/utils/ex/kq_ex.dart';
import 'package:kq_flutter_core_widget/utils/log.dart';

/// 第三方登录调用原生工具类
class ThirdLoginNativeUtil {
  static const _methodChannelName = 'flutter.keqiang.com/thirdLogin';

  static const _platformMethod = MethodChannel(_methodChannelName);

  /// 微信授权登录
  ///
  /// 适配：Android、IOS
  static Future<AuthLoginResult> wxAuthLogin(String appId,
      {String? universalLink}) async {
    try {
      var value = await _platformMethod.invokeMethod(
          'wxAuth', {'appId': appId, 'universalLink': universalLink});
      if (value == null) {
        return AuthLoginResult(code: ResultCode.failed);
      }

      var code = value['code'];
      if (code == '0') {
        var authCode = value['authCode'];
        return AuthLoginResult(code: ResultCode.success, authCode: authCode);
      } else if (code == '1') {
        return AuthLoginResult(code: ResultCode.failed);
      } else if (code == '-1') {
        return AuthLoginResult(code: ResultCode.cancel);
      } else if (code == '2') {
        return AuthLoginResult(code: ResultCode.notInstall);
      } else {
        return AuthLoginResult(code: ResultCode.failed);
      }
    } on PlatformException catch (e) {
      debugPrint(e.message);
      return AuthLoginResult(code: ResultCode.failed);
    } catch (e) {
      Log.e(e);
      return AuthLoginResult(code: ResultCode.failed);
    }
  }

  /// QQ授权登录
  ///
  /// 适配：Android、IOS
  static Future<QQAuthLoginResult> qqAuthLogin(String appId) async {
    try {
      var value =
          await _platformMethod.invokeMethod('qqAuth', {'appId': appId});
      if (value == null) {
        return QQAuthLoginResult(code: ResultCode.failed);
      }

      var code = value['code'];
      if (code == '0') {
        var openId = value['openId'];
        var accessToken = value['accessToken'];
        return QQAuthLoginResult(
            code: ResultCode.success, openId: openId, accessToken: accessToken);
      } else if (code == '1') {
        return QQAuthLoginResult(code: ResultCode.failed);
      } else if (code == '-1') {
        return QQAuthLoginResult(code: ResultCode.cancel);
      } else if (code == '2') {
        return QQAuthLoginResult(code: ResultCode.notInstall);
      } else {
        return QQAuthLoginResult(code: ResultCode.failed);
      }
    } on PlatformException catch (e) {
      debugPrint(e.message);
      return QQAuthLoginResult(code: ResultCode.failed);
    } catch (e) {
      Log.e(e);
      return QQAuthLoginResult(code: ResultCode.failed);
    }
  }

  /// 钉钉授权登录
  ///
  /// 适配：Android
  static Future<AuthLoginResult> ddAuthLogin(String appId) async {
    try {
      var value =
          await _platformMethod.invokeMethod('ddAuth', {'appId': appId});
      if (value == null) {
        return AuthLoginResult(code: ResultCode.failed);
      }

      var code = value['code'];
      if (code == 0) {
        var authCode = value['authCode'];
        return AuthLoginResult(code: ResultCode.success, authCode: authCode);
      } else if (code == -2) {
        return AuthLoginResult(code: ResultCode.cancel);
      } else if (code == -3) {
        return AuthLoginResult(code: ResultCode.notInstall);
      } else {
        return AuthLoginResult(code: ResultCode.failed);
      }
    } on PlatformException catch (e) {
      debugPrint(e.message);
      return AuthLoginResult(code: ResultCode.failed);
    } catch (e) {
      Log.e(e);
      return AuthLoginResult(code: ResultCode.failed);
    }
  }

  /// 微信文本内容分享
  ///
  /// 适配：Android、IOS
  ///
  /// [content]为分享的文本内容
  ///
  /// [title]为分享内容标题
  ///
  /// [desc]为分享内容的描述信息
  ///
  /// [icon]为分享内容的缩略图
  static Future<ResultCode> shareTextOnWx(String appId, String content,
      {String? title, String? desc, Uint8List? icon, String? universalLink}) {
    var arguments = {
      'appId': appId,
      'content': content,
      'title': title,
      'desc': desc,
      'icon': icon,
      'universalLink': universalLink ?? '',
    };
    return _share('shareWxTextData', arguments);
  }

  /// 微信网页内容分享
  ///
  /// 适配：Android、IOS
  ///
  /// [url]为分享的网页链接
  ///
  /// [title]为分享内容标题
  ///
  /// [desc]为分享内容的描述信息
  ///
  /// [icon]为分享内容的缩略图
  /// [universalLink] 微信开发者Universal Link
  static Future<ResultCode> shareWebOnWx(String appId, String url,
      {String? title, String? desc, Uint8List? icon, String? universalLink}) {
    var arguments = {
      'appId': appId,
      'url': url,
      'title': title,
      'desc': desc,
      'icon': icon,
      'universalLink': universalLink ?? ''
    };
    return _share('shareWxWebData', arguments);
  }

  /// 微信图片内容分享
  ///
  /// 适配：Android、IOS
  ///
  /// [localPath]为分享的图片本地路径，[bytes]为分享的图片字节数组。分享时传其中一个即可
  static Future<ResultCode> shareImageOnWx(String appId,
      {String? localPath, Uint8List? bytes}) {
    var arguments = {
      'appId': appId,
      'localPath': localPath,
      'bytes': bytes,
    };
    return _share('shareWxImageData', arguments);
  }

  /// QQ文本内容分享
  ///
  /// 适配：Android、IOS
  ///
  /// [content]为分享的文本内容
  static Future<ResultCode> shareTextOnQQ(String appId, String content) {
    var arguments = {
      'appId': appId,
      'content': content,
    };
    return _share('shareQQTextData', arguments);
  }

  /// QQ网页内容分享
  ///
  /// 适配：Android、IOS
  ///
  /// [url]为分享的网页链接
  ///
  /// [title]为分享内容标题
  ///
  /// [desc]为分享内容的描述信息
  ///
  /// [iconUrl]为分享内容的缩略图, 本地路径或网络地址
  static Future<ResultCode> shareWebOnQQ(String appId, String url,
      {String? title, String? desc, String? iconUrl}) {
    var arguments = {
      'appId': appId,
      'url': url,
      'title': title.isNullOrEmpty ? url : title,
      // 不传，QQ直接显示‘摘要’两个字，因此默认传一个空格符
      'desc': desc.isNullOrEmpty ? ' ' : desc,
      'iconUrl': iconUrl,
    };
    return _share('shareQQWebData', arguments);
  }

  /// QQ图片内容分享
  ///
  /// 适配：Android、IOS
  ///
  /// [localPath]为分享的图片本地路径
  static Future<ResultCode> shareImageOnQQ(String appId, String localPath) {
    var arguments = {
      'appId': appId,
      'localPath': localPath,
    };
    return _share('shareQQImageData', arguments);
  }

  /// 钉钉文本内容分享
  ///
  /// 适配：Android、IOS
  ///
  /// [content]为分享的文本内容
  ///
  /// [title]为分享内容标题
  ///
  /// [icon]为分享内容的缩略图字节对象，此对象太大无法拉起钉钉，目前测试6KB就不行，建议使用[iconUrl]
  ///
  /// [iconUrl]为分享内容的缩略图url
  ///
  /// [msgUrl]为消息链接
  ///
  /// [msgContent]为消息内容
  static Future<ResultCode> shareTextOnDD(String appId, String content,
      {String? title,
      Uint8List? icon,
      String? iconUrl,
      String? msgUrl,
      String? msgContent}) {
    var arguments = {
      'appId': appId,
      'content': content,
      'title': title,
      'icon': icon,
      'iconUrl': iconUrl,
      'msgUrl': msgUrl,
      'msgContent': msgContent,
    };
    return _share('shareDDTextData', arguments);
  }

  /// 钉钉网页内容分享
  ///
  /// 适配：Android、IOS
  ///
  /// [url]为分享的网页链接
  ///
  /// [title]为分享内容标题
  ///
  /// [icon]为分享内容的缩略图字节对象，此对象太大无法拉起钉钉，目前测试6KB就不行，建议使用[iconUrl]
  ///
  /// [iconUrl]为分享内容的缩略图url
  ///
  /// [msgUrl]为消息链接
  ///
  /// [msgContent]为消息内容
  static Future<ResultCode> shareWebOnDD(String appId, String url,
      {String? title,
      Uint8List? icon,
      String? iconUrl,
      String? msgUrl,
      String? msgContent}) {
    var arguments = {
      'appId': appId,
      'url': url,
      'title': title,
      'icon': icon,
      'iconUrl': iconUrl,
      'msgUrl': msgUrl,
      'msgContent': msgContent,
    };
    return _share('shareDDWebData', arguments);
  }

  /// 钉钉图片内容分享
  ///
  /// 适配：Android、IOS
  ///
  /// [localPath]为分享的图片本地路径，[bytes]为分享的图片字节数组，[url]为分享的图片网络地址。分享时传其中一个即可。
  static Future<ResultCode> shareImageOnDD(String appId,
      {String? localPath, Uint8List? bytes, String? url}) {
    var arguments = {
      'appId': appId,
      'url': url,
      'localPath': localPath,
      'bytes': bytes,
    };
    return _share('shareDDImageData', arguments);
  }

  static Future<ResultCode> _share(String method, dynamic arguments) async {
    try {
      var result = await _platformMethod.invokeMethod(method, arguments);
      if (result == 0) {
        return ResultCode.success;
      } else if (result == -2) {
        return ResultCode.cancel;
      } else if (result == -3) {
        return ResultCode.notInstall;
      } else {
        return ResultCode.failed;
      }
    } on PlatformException catch (e) {
      debugPrint(e.message);
      return ResultCode.failed;
    } catch (e) {
      Log.e(e);
      return ResultCode.failed;
    }
  }
}

enum ResultCode {
  /// 操作成功
  success,

  /// 操作失败
  failed,

  /// app未安装
  notInstall,

  /// 用户取消
  cancel
}

/// 授权登录结果
class AuthLoginResult {
  ResultCode code;
  String? authCode;

  AuthLoginResult({required this.code, this.authCode});
}

/// QQ授权登录结果
class QQAuthLoginResult {
  ResultCode code;

  /// 用户唯一Id
  String? openId;

  /// 访问用户用户数据令牌
  String? accessToken;

  QQAuthLoginResult({required this.code, this.openId, this.accessToken});
}
